/* #include<iostream>
#include<sstream>
#include</usr/include/jsoncpp/json/json.h>
#include<string>

std::string serialize()
{
    //1.创建一个json value对象
    Json::Value root;
    root["姓名"]="李四";
    root["年龄"]=18;
    root["成绩"].append(78);
    root["成绩"].append(80);
    root["成绩"].append(86);
    //2.创建一个工厂类StreamWriterBuilder对象
    Json::StreamWriterBuilder builder;
    //3.实例化一个StreamWriter对象
    Json::StreamWriter* sw=builder.newStreamWriter();
    //4.进行序列化
    std::stringstream ss;
    int ret=sw->write(root,&ss);
    if(ret!=0)
    {
        std::cout<<"序列化失败"<<std::endl;
        return "error";
    }
    std::cout<<ss.str()<<std::endl;
    return ss.str();
}

bool unserialize(std::string str)
{
    //1.创建一个CharReaderBuilder工厂类对象
    //2.实例化出一个CharReader对象
    //3.创建一个Json::Value对象用来接收反序列化后的对象
    //4.调用parse接口进行反序列化
    Json::CharReaderBuilder crb;
    Json::CharReader* cr=crb.newCharReader();
    Json::Value root;
    std::string err;
    bool ret=cr->parse(str.c_str(),str.c_str()+str.size(),&root,&err);
    if(ret==false)
    {
        std::cout<<"unserialize fail!!"<<std::endl;
        return false;
    }
    std::cout<<root["姓名"].asString()<<std::endl;
    std::cout<<root["年龄"].asInt()<<std::endl;
    int sz=root["成绩"].size();
    for(int i=0;i<sz;i++)
    {
        std::cout<<root["成绩"][i]<<std::endl;
    }
    return true;
}
int main()
{
    std::string ret=serialize();
    if(ret.c_str()!="error")
    {
        unserialize(ret);
    }
    return 0;
} */

/* #include<iostream>
#include<cstdio>
#include<unistd.h>
#include<pthread.h>
#include<vector>

pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;  // 静态初始化锁
int ticket=1000;
void* consum(void* args)
{
    char* name=static_cast<char*>(args);
    //一个模拟抢票的代码片段：
    sleep(1);
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if ( ticket > 0 ) {
            usleep(1000);
            printf("%s sells ticket:%d\n", name, --ticket);
            pthread_mutex_unlock(&mutex);
        } else {
            pthread_mutex_unlock(&mutex);
            break;
        }
        usleep(10000);
    }
    
    return nullptr;
}
void* produce(void* args)
{
    char* name=static_cast<char*>(args);
    //一个模拟加票的代码片段：
    
    while(1)
    {
        pthread_mutex_lock(&mutex);
        if ( ticket <10 ) {
            usleep(10000);
            printf("%s add ticket:%d\n", name, ++ticket);
            pthread_mutex_unlock(&mutex);
        } else {
            pthread_mutex_unlock(&mutex);
        }
        usleep(123);
    }
    
    return nullptr;
}

int main()
{
    std::vector<pthread_t> arr;
    int num=10;
    for(int i=0;i<num;i++)
    {
        pthread_t tid;
        char* name=(char*)malloc(64);
        snprintf(name,64,"thread-%d",i);
        if(i%2==0)
        {
            int ret=pthread_create(&tid,nullptr,consum,(void*)name);
        }
        else
        {
            int ret=pthread_create(&tid,nullptr,produce,(void*)name);
        } 
        arr.push_back(tid);
    }
    for(int i=0;i<10;i++)
    {
        pthread_join(arr[i],nullptr);
    }
    return 0;
} */

#include<iostream>
#include</usr/include/mysql/mysql.h>
#include<cstdio>
#include<string>

#define HOST "127.0.0.1"
#define USER "root"
#define PASSWORD "200411@Yjh"
#define DB "gobang"
#define CSNAME "utf8"
#define PORT 3306
int main()
{
    //1.创建一个mysql句柄
    MYSQL * mysql=mysql_init(nullptr);
    //2.连接服务器
    MYSQL * res=mysql_real_connect(mysql,HOST,USER,PASSWORD,DB,PORT,nullptr,0);
    if(res==nullptr)
    {
        printf("mysql connect failed :%s\n",mysql_error(mysql));
        mysql_close(mysql);
        return -1;
    }
    //3.设置客户端字符集
    int ret=mysql_set_character_set(mysql, CSNAME);
    if(ret!=0)
    {
        printf("mysql_set_character_set failed :%s\n",mysql_error(mysql));
        mysql_close(mysql);
        return -1;
    }
    //4.选择需要操作的数据库：
    //mysql_select_db(MYSQL *mysql,  const char *db)
    //5.执行sql语句
    //std::string query="insert stu values(null,'小明',18,58,68,98);";
    std::string query="select * from stu";
    if(mysql_query(mysql,query.c_str())!=0)
    {
        printf("mysql_query failed :%s\n",mysql_error(mysql));
        printf("query:%s",query.c_str());
        mysql_close(mysql);
        return -1;
    }
    //6.如果是查询的话将查询结果保存到本地
    MYSQL_RES *rest=mysql_store_result(mysql);
    if(rest==nullptr)
    {
        printf("mysql_store_result failed :%s\n",mysql_error(mysql));
        mysql_close(mysql);
        return -1;
    }
    //7.获取结果集中的结果条数
    int rows=mysql_num_rows(rest);
    int fields=mysql_num_fields(rest);
    //8.遍历结果集
    for(int i=0;i<rows;i++)
    {
        MYSQL_ROW row=mysql_fetch_row(rest);
        for(int j=0;j<fields;j++)
        {
            printf("%s\t",row[j]);
        }
        printf("\n");
    }
    //9.释放结果集：
    mysql_free_result(rest);
    //1.关闭连接，销毁句柄
    mysql_close(mysql);
    return 0;
}

